#include "app_uart_drv.h"
#include <string.h>
#include "crc16.h"

/***************************************************************************************************
 * variable definition
 **************************************************************************************************/
                 
#define UART_DRV_SIZE    18
                         
uint8_t drv1_RecData[UART_DRV_SIZE];
uint8_t drv2_RecData[UART_DRV_SIZE];

volatile bool drv1_rxComplete = false;
volatile bool drv1_txComplete = false;

volatile bool drv2_rxComplete = false;
volatile bool drv2_txComplete = false;

// 协议常量定义

#define FRAME_HEADER_DRV 0xB7
#define CMD_MCU_DOWN_DRV 0x03  

uint8_t drv_SendData[UART_DRV_SIZE];

static uint16_t drv1_aimSpeed = 0;
static bool     drv1_aimSpeedNeedSet = false;
/***************************************************************************************************
 * function declaration
 **************************************************************************************************/

/* Uart rx callback function */
void Uart_DRV1_RxCallback(void *LINFLexDState, uart_event_t event, void *userData); 
void Uart_DRV2_RxCallback(void *LINFLexDState, uart_event_t event, void *userData);

/* Uart tx callback function */
void Uart_DRV1_TxCallback(void *LINFLexDState, uart_event_t event, void *userData);  
void Uart_DRV2_TxCallback(void *LINFLexDState, uart_event_t event, void *userData);

/***************************************************************************************************
 * Scheduling function definition
 ***************************************************************************************************/

 void app_uartDrvInit() {
    
    LINFlexD_UART_DRV_Init(INST_UART_DRV1,&linflexd_uart_config1_State,&linflexd_uart_config1); 
    LINFlexD_UART_DRV_Init(INST_UART_DRV2,&linflexd_uart_config2_State,&linflexd_uart_config2);
 
    LINFlexD_UART_DRV_InstallRxCallback(INST_UART_DRV1, Uart_DRV1_RxCallback, NULL);
    LINFlexD_UART_DRV_InstallRxCallback(INST_UART_DRV2, Uart_DRV2_RxCallback, NULL);
     
    LINFlexD_UART_DRV_InstallTxCallback(INST_UART_DRV1, Uart_DRV1_TxCallback, NULL);  
    LINFlexD_UART_DRV_InstallTxCallback(INST_UART_DRV2, Uart_DRV2_TxCallback, NULL);
     
    INT_SYS_EnableIRQ(LINFlexD1_IRQn);
    INT_SYS_EnableIRQ(LINFlexD2_IRQn);

    /* Send received data */
    LINFlexD_UART_DRV_ReceiveData(INST_UART_DRV1, drv1_RecData, UART_DRV_SIZE);
    LINFlexD_UART_DRV_ReceiveData(INST_UART_DRV2, drv2_RecData, UART_DRV_SIZE);
}

// ====================== 报文发送函数 ======================
// 通用帧构造函数
static void build_frame(uint8_t *buf, uint8_t infoId) {
    buf[0] = FRAME_HEADER_DRV;
    
    for(uint8_t index = 1; index < UART_DRV_SIZE - 1; index += 1) {
        buf[index] = 0x0;
    }
    
    buf[1] = infoId;
}    

static void build_frameSum(uint8_t *buf) {
    
    buf[UART_DRV_SIZE-1] = checksum(buf + 1, UART_DRV_SIZE-2);
} 

void app_uartDrvSendSpeedInfo(uint8_t instance, uint16_t aimSpeed) {
    drv1_aimSpeed = aimSpeed; 
    
    build_frame(drv_SendData, CMD_MCU_DOWN_DRV);
    drv_SendData[2] = 0x01;
    drv_SendData[3] = (uint8_t)(aimSpeed >> 8);  
    drv_SendData[4] = (uint8_t)aimSpeed;

    build_frameSum(drv_SendData);   

    LINFlexD_UART_DRV_SendData(instance, drv_SendData, UART_DRV_SIZE);   
}

void app_uartDrvSearchInfo(uint8_t instance) {
   
    build_frame(drv_SendData, CMD_MCU_DOWN_DRV);
    drv_SendData[2] = 0x00;

    build_frameSum(drv_SendData);   
    
    LINFlexD_UART_DRV_SendData(instance, drv_SendData, UART_DRV_SIZE);   
    
}

void app_uartDrvSendStopInfo(uint8_t instance) {
    
    build_frame(drv_SendData, CMD_MCU_DOWN_DRV);
    drv_SendData[2] = 0x03;

    build_frameSum(drv_SendData);  
    LINFlexD_UART_DRV_SendData(instance, drv_SendData, UART_DRV_SIZE);   
}


// ====================== 报文解析函数 ======================
void app_uartDrvReceiveData() {
    
    if (drv1_rxComplete) {
        drv1_rxComplete = false;
        LINFlexD_UART_DRV_ReceiveData(INST_UART_DRV1, drv1_RecData, UART_DRV_SIZE);
        
        // 进行解析    
        uint8_t length = sizeof(drv1_RecData) / sizeof(drv1_RecData[0]);
        
        if (length < UART_DRV_SIZE)
            return;
        
        if (drv1_RecData[0]!=FRAME_HEADER_DRV)
            return;
        
        if (crc_16(drv1_RecData, UART_DRV_SIZE-1)!=drv1_RecData[UART_DRV_SIZE-1])
            return;
        
        if (drv1_RecData[1] == 0) { // 查询
            // no func
        } else if (drv1_RecData[1] == 1) { // 速度控制模式

        }
    }
}
 
 /* Uart rx callback function */
void Uart_DRV1_RxCallback(void *LINFLexDState, uart_event_t event, void *userData)
{
    (void)LINFLexDState;
    (void)userData;
    if (event == UART_EVENT_END_TRANSFER)
    {
        drv1_rxComplete = true;
    }
}

void Uart_DRV2_RxCallback(void *LINFLexDState, uart_event_t event, void *userData)
{
    (void)LINFLexDState;
    (void)userData;
    if (event == UART_EVENT_END_TRANSFER)
    {
        drv2_rxComplete = true;
    }
}

/* Uart tx callback function */
void Uart_DRV1_TxCallback(void *LINFLexDState, uart_event_t event, void *userData)
{
    (void)LINFLexDState;
    (void)userData;
    if (event == UART_EVENT_END_TRANSFER)
    {
        drv1_txComplete = true;             
    }
}

void Uart_DRV2_TxCallback(void *LINFLexDState, uart_event_t event, void *userData)
{
    (void)LINFLexDState;
    (void)userData;
    if (event == UART_EVENT_END_TRANSFER)
    {
        drv2_txComplete = true;                
    }
}
 


